﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class MainForm : Form
    {
        public ChooseRobotService.robotData chosenRobot;
        public SensorService.sensorData chosenSensor; 
        public LinkedList<ChooseRobotService.robotData> RoboterLList;
        public LinkedList<SensorService.sensorData> SensorList; 
        public String user = "TestUser"; 

        public MainForm()
        {
            InitializeComponent();
            chosenRobot = null;
            RoboterLList = new LinkedList<WindowsFormsApplication3.ChooseRobotService.robotData>();
            SensorList = new LinkedList<WindowsFormsApplication3.SensorService.sensorData>(); 
        }

        private void buttonGetList_Click(object sender, EventArgs e)
        {
            this.roboListe.Items.Clear(); 
            ChooseRobotService.ChooseRoboterServiceClient robotServiceClient = new ChooseRobotService.ChooseRoboterServiceClient();

            ChooseRobotService.getRobotListRequest request = new WindowsFormsApplication3.ChooseRobotService.getRobotListRequest();

            ChooseRobotService.robotData[] robotList = robotServiceClient.getRobotList(); 
            foreach (ChooseRobotService.robotData item in robotList)
            {
                roboListe.Items.Add(item.robotID);
                RoboterLList.AddLast(item); 
            }

        }

        private void roboListe_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selected = ((ListBox)sender).SelectedIndex;
            if (selected != -1)
            {
                String selectedString = ((ListBox)sender).Items[selected].ToString();


                foreach (ChooseRobotService.robotData item in this.RoboterLList)
                {
                    if (item.robotID.Equals(selectedString))
                    {
                        this.chosenRobot = item;
                    }
                }

                if (this.chosenRobot != null)
                {
                    this.labelRoboID.Text = this.chosenRobot.robotID;
                    this.textRoboBeschreibung.Text = this.chosenRobot.description;
                    this.buttonBucheRobo.Enabled = true;

                }
            }
        }

        private void buttonBucheRobo_Click(object sender, EventArgs e)
        {
            ChooseRobotService.ChooseRoboterServiceClient client = new WindowsFormsApplication3.ChooseRobotService.ChooseRoboterServiceClient();
            if (client.bookRobot(this.chosenRobot.robotID, this.user))
            {
                MessageBox.Show("Roboter wurde gebucht!");
                this.buttonBucheRobo.Enabled = false;
                this.roboListe.Enabled = false; 
                this.buttonCancelRobo.Enabled = true;
                this.movementPanel.Enabled = true; 

                // Dritte Tabpage aktivieren 
                this.panelSensorInformation.Enabled = true;
                this.buttonGetSensorList.Enabled = true;
                this.listBoxSensors.Enabled = true; 
            }
            else
            {
                MessageBox.Show("Roboter wurde nicht gebucht!");
            }
        }

        private void buttonCancelRobo_Click(object sender, EventArgs e)
        {
            ChooseRobotService.ChooseRoboterServiceClient client = new WindowsFormsApplication3.ChooseRobotService.ChooseRoboterServiceClient(); 
            if (client.cancelRobot(this.chosenRobot.robotID, this.user))
            {
                MessageBox.Show("Roboter wurde abgemeldet!"); 
                this.buttonBucheRobo.Enabled = true; 
                this.roboListe.Enabled = true; 
                this.buttonCancelRobo.Enabled = false;
                this.movementPanel.Enabled = false;

                // Dritte Tabpage aktivieren 
                this.panelSensorInformation.Enabled = false;
                this.buttonGetSensorList.Enabled = false;
                this.listBoxSensors.Enabled = false; 

                // Sensorliste löschen: 
                this.SensorList.Clear();
                this.listBoxSensors.Items.Clear(); 
            } else
            {
                MessageBox.Show("Konnte nicht stornieren"); 
            }
        }

        private void forwardButton_Click(object sender, EventArgs e)
        {
            MovementService.MovementServiceClient moveClient = new WindowsFormsApplication3.MovementService.MovementServiceClient();
            bool result = moveClient.forward(this.chosenRobot.robotID);
            if (result)
            {
                this.movementLogText.Items.Add("Befehl forward erfolgreich");
            }
            else
            {
                this.movementLogText.Items.Add("Befehl forward nicht erfolgreich");
            }
        }

        private void leftButton_Click(object sender, EventArgs e)
        {
            MovementService.MovementServiceClient moveClient = new WindowsFormsApplication3.MovementService.MovementServiceClient();
            bool result = moveClient.turnLeft(this.chosenRobot.robotID);
            if (result)
            {
                this.movementLogText.Items.Add("Befehl turnLeft erfolgreich");
            }
            else
            {
                this.movementLogText.Items.Add("Befehl turnLeft nicht erfolgreich");
            }
        }

        private void cancelMoveButton_Click(object sender, EventArgs e)
        {
            MovementService.MovementServiceClient moveClient = new WindowsFormsApplication3.MovementService.MovementServiceClient();
            bool result = moveClient.stop(this.chosenRobot.robotID);
            if (result)
            {
                this.movementLogText.Items.Add("Befehl Stop erfolgreich");
            }
            else
            {
                this.movementLogText.Items.Add("Befehl Stop nicht erfolgreich");
            }            
        }

        private void rightButton_Click(object sender, EventArgs e)
        {
            MovementService.MovementServiceClient moveClient = new WindowsFormsApplication3.MovementService.MovementServiceClient();
            bool result = moveClient.turnRight(this.chosenRobot.robotID);
            if (result)
            {
                this.movementLogText.Items.Add("Befehl turnRight erfolgreich");
            }
            else
            {
                this.movementLogText.Items.Add("Befehl turnRight nicht erfolgreich");
            }   
        }

        private void backwardButton_Click(object sender, EventArgs e)
        {
            MovementService.MovementServiceClient moveClient = new WindowsFormsApplication3.MovementService.MovementServiceClient();
            bool result = moveClient.backward(this.chosenRobot.robotID);
            if (result)
            {
                this.movementLogText.Items.Add("Befehl backward erfolgreich");
            }
            else
            {
                this.movementLogText.Items.Add("Befehl backward nicht erfolgreich");
            }   
        }

        private void buttonGetSensorList_Click(object sender, EventArgs e)
        {
            SensorService.SensorServiceClient sensorClient = new WindowsFormsApplication3.SensorService.SensorServiceClient();
            SensorService.sensorData[] list =  sensorClient.getSensorList(this.chosenRobot.robotID);
            
            foreach (SensorService.sensorData item in list)
            {
                
                this.listBoxSensors.Items.Add(item.id);
                SensorList.AddLast(item); 
            }
        }

        private void listBoxSensors_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selected = ((ListBox)sender).SelectedIndex;
            if (selected != -1)
            {
                String selectedString = ((ListBox)sender).Items[selected].ToString();


                foreach (SensorService.sensorData item in this.SensorList)
                {
                    if (item.id.Equals(selectedString))
                    {
                        this.chosenSensor = item;
                    }
                }

                if (this.chosenSensor != null)
                {
                    this.labelSensorsID.Text = this.chosenSensor.id;
                    this.textBoxSensorDescription.Text = this.chosenSensor.description;
                    this.labelSensorsType.Text = this.chosenSensor.type;
                    this.buttonGetSensorData.Enabled = true;

                }
            }
        }

        private void buttonGetSensorData_Click(object sender, EventArgs e)
        {
            SensorService.SensorServiceClient sensorClient = new WindowsFormsApplication3.SensorService.SensorServiceClient();
            SensorService.sensorData sd; 
            sd = sensorClient.getSensorData(this.chosenRobot.robotID, this.chosenSensor.id); 
        
            this.listBoxSensorData.Items.Add("Wert von Sensor: " + this.chosenSensor.id + " : " + sd.value); 
        }

    }
}
